package com.lcu.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcu.reggie.dto.DishDto;
import com.lcu.reggie.entity.Dish;
import com.lcu.reggie.entity.DishFlavor;
import com.lcu.reggie.mapper.DishMapper;
import com.lcu.reggie.service.DishFlavorService;
import com.lcu.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时保存对应的口味数据
     *
     * @param dishDto
     */
    @Transactional  //开启事务
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品基本信息到dish
        this.save(dishDto);
        Long dishId = dishDto.getId();//菜品id

        //保存菜品口味数据到dish_flavor
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id查询菜品基本信息及口味信息
     *
     * @param id
     * @return
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //创建DishDto
        DishDto dishDto = new DishDto();

        //1.先从dish中查询菜品基本信息
        Dish dish = this.getById(id);

        //拷贝dish数据
        BeanUtils.copyProperties(dish, dishDto);

        //2.再从dish_flavor中查询口味信息
        //创建条件构造器
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(list);

        return dishDto;
    }

    /**
     * 修改菜品基本数据及口味信息
     *
     * @param dishDto
     */
    @Override
    @Transactional //保证事务一致性
    public void updateWithFlavor(DishDto dishDto) {
//        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
//        queryWrapper.eq(Dish::getId,dishDto.getId());
//        this.update(queryWrapper);
        this.updateById(dishDto);

        //清理当前菜品的口味数据---dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //添加当前提交的口味数据---dish_flavor表的insert操作
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());

        dishFlavorService.saveBatch(flavors);

    }

    /**
     * 根据id删除菜品基本信息及口味信息
     * @param ids
     */
    @Transactional
    @Override
    public void deleteWithFlavor(Long[] ids) {
        for (Long id : ids) {
            this.removeById(id);
            LambdaQueryWrapper<DishFlavor> queryWrapper=new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId,id);
            dishFlavorService.remove(queryWrapper);
        }


    }


}
